const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const env = process.env.NODE_ENV;
const isProduction = env === "production";

const publicPath = isProduction ? "/" : "/";

// 公共代码抽离
function splitChunks(config) {
  config.optimization = {
    splitChunks: {
      cacheGroups: {
        vendor: {
          chunks: "all",
          test: /node_modules/,
          name: "vendor",
          minChunks: 1,
          maxInitialRequests: 5,
          minSize: 0,
          priority: 100
        },
        common: {
          chunks: "all",
          test: /[\\/]src[\\/]js[\\/]/,
          name: "common",
          minChunks: 2,
          maxInitialRequests: 5,
          minSize: 0,
          priority: 60
        },
        styles: {
          name: "styles",
          test: /\.(sa|sc|c|le)ss$/,
          chunks: "all",
          enforce: true
        },
        runtimeChunk: {
          name: "manifest"
        }
      }
    }
  };
}

// 代码压缩
function uglifyJs(config) {
  config.plugins.push(
    new UglifyJsPlugin({
      uglifyOptions: {
        compress: {
          drop_debugger: true,
          drop_console: true,
          pure_funcs: ["console.log"]
        }
      },
      sourceMap: false,
      parallel: true
    })
  );
}

module.exports = {
  publicPath,
  assetsDir:'static',
  lintOnSave: false,
  productionSourceMap: false,
  runtimeCompiler: true,
  devServer: {
    open: true,
    host: "0.0.0.0",
    port: 8000,
    proxy: {
      [process.env.VUE_APP_BASE_API]: {
        target: `http://10.10.14.27:80`, // 内网
        changeOrigin: true,
        pathRewrite: {
          ["^" + process.env.VUE_APP_BASE_API]: ""
        }
      }
    }
  },
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          javascriptEnabled: true
        }
      }
    }
  },
  configureWebpack: (config) => {
    if (isProduction) {
      // 为生产环境修改配置...
      config.mode = "production";
      config["performance"] = {
        //打包文件大小配置
        maxEntrypointSize: 10000000,
        maxAssetSize: 30000000
      };
      // 公共代码抽离
      splitChunks(config);
      // 代码压缩
      uglifyJs(config);
    }
  }
};
